﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using WebSiteAdminTool.Common.Interfaces;
using System.Web.Security;

namespace WebSiteAdminTool.Web.Services.DTOs
{
    [DataContract]
    public class UserData : IIdentifiable, IValidatable
    {
        [DataMember]
        public object UserId { get; set; }
        [DataMember]
        public string UserName { get; set; }
        [DataMember]
        public string Email { get; set; }
        [DataMember]
        public string Comment { get; set; }
        [DataMember]
        public DateTime CreationDate { get; set; }
        [DataMember]
        public DateTime LastActivityDate { get; set; }
        [DataMember]
        public DateTime LastLoginDate { get; set; }
        [DataMember]
        public DateTime LastPasswordChangedDate { get; set; }
        [DataMember]
        public bool IsApproved { get; set; }
        [DataMember]
        public bool IsLockedOut { get; set; }
        [DataMember]
        public bool IsOnline { get; set; }
        [DataMember]
        public string UserRoles { get; set; }
        [DataMember]
        public string PasswordQuestion { get; set; }

        public UserData(System.Web.Security.MembershipUser u, string[] userRoles = null)
        {
            this.UserId = u.ProviderUserKey;
            this.UserName = u.UserName;
            this.Email = u.Email;
            this.Comment = u.Comment;
            this.CreationDate = u.CreationDate;
            this.LastActivityDate = u.LastActivityDate;
            this.LastLoginDate = u.LastLoginDate;
            this.LastPasswordChangedDate = u.LastPasswordChangedDate;
            this.IsApproved = u.IsApproved;
            this.IsLockedOut = u.IsLockedOut;
            this.IsOnline = u.IsOnline;
            this.PasswordQuestion = u.PasswordQuestion;
            //this.PasswordAnswer = u.PasswordAnswer;
            // resolve user roles
            if (null != userRoles && userRoles.Length != 0)
            {
                Array.Sort<string>(userRoles);
                this.UserRoles = userRoles.Aggregate((c, n) => c + "," + n);
            }
        }

        #region IIdentifiable implementation

        [DataMember]
        public object Id
        {
            get { return this.UserId; }
            set { this.UserId = value; }
        }

        #endregion

        #region IValidatable implementation

        public bool Validate()
        {
            if (string.IsNullOrWhiteSpace(UserName) ||
                (Membership.RequiresQuestionAndAnswer && string.IsNullOrWhiteSpace(PasswordQuestion)))
            {
                ThrowValidationException();
            }

            return true;
        }

        //internal bool ValidateNamePassword(string userName, string password)
        //{
        //    if (!Membership.ValidateUser(userName, password))
        //    {
        //        ThrowValidationException();
        //    }

        //    return true;
        //}

        private static void ThrowValidationException()
        {
            throw new InvalidOperationException("The given user instance is not properly instantiated.");
        }

        #endregion
    }
}